package com.yuanqi.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanqi.Common.config.ThreadLocalConfig;
import com.yuanqi.Mapper.SupportMapper;
import com.yuanqi.Common.Pojo.Dto.SupportDto;
import com.yuanqi.Common.Pojo.entity.Support;
import com.yuanqi.Service.SupportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SupportServiceImpl extends ServiceImpl<SupportMapper, Support> implements SupportService {

    @Resource
    SupportMapper supportMapper;
    @Resource
    ThreadLocalConfig threadLocalConfig;
    @Override
    public void support(SupportDto supportDto) {
        //获取用户Id
        Long userId = Long.valueOf(threadLocalConfig.get().get("userId").toString());
        //查询关注表中是否有记录
        LambdaQueryWrapper<Support> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Support::getPostId,supportDto.getPostId());
        wrapper.eq(Support::getUserId,userId);
        Support support = supportMapper.selectOne(wrapper);
        //为空则添加
        if(support==null)
        {
            Support build = Support.builder().type(supportDto.getType()).createTime(System.currentTimeMillis()/1000).postId(supportDto.getPostId()).userId(userId).build();
             supportMapper.insert(build);
        }
        else {
            support.setType(supportDto.getType());
            supportMapper.updateById(support);
        }
    }
}
